﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：Configuration_ModelPocoViewModel
// 创 建 者：杨程
// 创建时间：2021/10/14 17:07:26
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using V.CodeGenerator.CreateCodes.Model;
using V.CodeGenerator.Model;
using Vampirewal.Core;
using Vampirewal.Core.Interface;
using Vampirewal.Core.SimpleMVVM;
using Vampirewal.Core.Tools;
using Vampirewal.Core.IoC;
using Vampirewal.Core.WpfTheme.WindowStyle;

namespace V.CodeGenerator.ViewModel
{
    [VampirewalIoCRegister(ViewModelKey.Configuration_ModelPocoViewModel, RegisterType.ViewModel)]
    public class Configuration_ModelPocoViewModel:BillVM<ProjectPoco>
    {
        private IDialogMessage Dialog { get; set; }
        public Configuration_ModelPocoViewModel(IDataContext dc,IDialogMessage dialog) : base(dc)
        {
            //构造函数
            Dialog = dialog;
        }

        public override void InitData()
        {
            HasModels = new ObservableCollection<ModelPoco>();
        }

        public override void PassData(object obj)
        {
            SetEntity(obj as ProjectPoco);


        }

        #region 属性
        private ModulePoco _TreeSelectItem;
        public ModulePoco TreeSelectItem
        {
            get { return _TreeSelectItem; }
            set { _TreeSelectItem = value; DoNotify(); }
        }


        public ObservableCollection<ModelPoco> HasModels { get; set; }

        public bool CanShow { get; set; }
        #endregion

        #region 公共方法

        #endregion

        #region 私有方法

        /// <summary>
        /// 重置HasModels集合
        /// </summary>
        private void ResetHasModels(List<ModelPoco> sources)
        {
            HasModels.Clear();
            HasModels = sources.ListToObservableCollection(HasModels);
        }
        #endregion

        #region 命令
        public RelayCommand<ModulePoco> SelectTreeItemsCommand => new RelayCommand<ModulePoco>((s) =>
        {
            //if (s.moduleFunctionType== ModuleFunctionType.Function)
            //{
            //    TreeSelectItem = s;

            //    var models = DC.Client.Queryable<ModelPoco>().Where(w => w.FunctionId == s.BillId).ToList();
            //    ResetHasModels(models);

            //    if (models.Any(a=>a.modelType== ModelType.BaseModel))
            //    {
            //        CanShow = true;
            //    }
            //}
            //else
            //{
            //    TreeSelectItem = null;
            //    HasModels.Clear();
            //    CanShow = false;
            //}
            
        });

        public RelayCommand CreateBaseModelCommand => new RelayCommand(() =>
        {
            if (TreeSelectItem==null)
            {
                return;
            }

            

            List<SimplePassData> simplePasses = new List<SimplePassData>() 
            {
                new SimplePassData()
                {
                    Key="Project",
                    Value=Entity
                },
                new SimplePassData()
                {
                    Key="ModuleFunction",
                    Value=TreeSelectItem
                }
            };


            var GetResult= Dialog.OpenDialogWindow(new DialogWindowSetting()
            {
                IsShowMaxButton=false,
                IsShowMinButton=false,
                UiView=Messenger.Default.Send<FrameworkElement>("GetView", ViewKey.AddOrEditModelPocoView),
                PassData= simplePasses

            }) as ModelPoco;

            if (GetResult!=null)
            {
                HasModels.Add(GetResult);
            }
        });


        public RelayCommand<ModelPoco> EditCommand => new RelayCommand<ModelPoco>((e) => 
        {
            if (TreeSelectItem == null)
            {
                return;
            }
            

            List<SimplePassData> simplePasses = new List<SimplePassData>()
            {
                new SimplePassData()
                {
                    Key="ModelPoco",
                    Value=e
                }
            };

            var GetResult = Dialog.OpenDialogWindow(new DialogWindowSetting()
            {
                IsShowMaxButton = false,
                IsShowMinButton = false,
                UiView = Messenger.Default.Send<FrameworkElement>("GetView", ViewKey.AddOrEditModelPocoView),
                PassData = simplePasses

            }) as ModelPoco;

            if (GetResult != null)
            {
                var old= HasModels.First(f => f.BillId == GetResult.BillId);
                HasModels.Remove(old);
                HasModels.Add(GetResult);
            }

        });
        #endregion
    }


    
}
